/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.batch.web.CommonBatchAction
   Module Description   :

   Date Created      : 2011/2/9
   Original Author   : jeffma
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.batch.web;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.yaodian100.core.admin.entity.Role;
import com.yaodian100.core.admin.entity.User;
import com.yaodian100.core.admin.service.UserService;
import com.yaodian100.core.admin.utils.AdminHelper;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.utils.DateUtil;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.core.menu.service.MenuService;
import com.yaodian100.ec.batch.entity.BatchDetail;
import com.yaodian100.ec.batch.entity.BatchEvent;
import com.yaodian100.ec.batch.entity.BatchEvent.Status;
import com.yaodian100.ec.batch.service.BatchEventService;
import com.yaodian100.ec.common.web.AbstractAction;

/**
 * @author jeffma
 * 
 */
public class CommonBatchAction extends AbstractAction {

	/** serialVersionUID */
	private static final long serialVersionUID = 4889179642482209603L;
	@Autowired
	protected BatchEventService batchEventService;
	@Autowired
	protected MenuService menuService;
	@Autowired
	protected UserService userService;

	private String paramEventId;
	private BatchEvent.Status paramStatus;
	private String paramBatchName;
	private Date paramCreateDTStart;
	private Date paramCreateDTEnd;
	private String paramCreateUser;

	private Menu memuType;
	private List<BatchEvent.Status> menuStatus = null;
	private List<String> menuTime;
	private List<User> menuPM;

	private List<BatchEvent> batchEvents;
	private String batchEventId;

	private String actorId;

	private String sortBy = "createDate";
	private String sortByType = "desc";

	private BatchEvent event;
	private Map<String, BatchDetail> detailMap;

	public String query() {

		if (paramCreateDTStart == null) {
			paramCreateDTStart = DateUtil.getTodayAddDays(-3).getTime();

		}

		if (paramCreateDTEnd == null) {
			paramCreateDTEnd = new Date();
		}

		return "list";
	}

	public String list() {
		CommonCriteria cri = new CommonCriteria();

		if (StringUtils.isNotBlank(paramEventId)) {
			cri.addRlike("oid", paramEventId);
		}
		if (paramStatus != null) {
			cri.addEq("status", paramStatus);
		}
		if (StringUtils.isNotBlank(paramCreateUser)) {
			cri.addEq("createUser", paramCreateUser);
		}
		if (StringUtils.isNotBlank(paramBatchName)) {
			cri.addEq("batchName", paramBatchName);
		}
		if (paramCreateDTStart != null) {
			cri.addGe("createDate", paramCreateDTStart);
		} else {
			paramCreateDTStart = DateUtil.getTodayAddDays(-3).getTime();
			cri.addGe("createDate", paramCreateDTStart);
		}
		if (paramCreateDTEnd != null) {
			cri.addLe("createDate", paramCreateDTEnd);
		} else {
			paramCreateDTEnd = new Date();
			cri.addLe("createDate", paramCreateDTEnd);
		}

		long entitySize = batchEventService.getListSize(cri).longValue();
		getPager().setTotalSize(entitySize);
		batchEvents = batchEventService.getList(getPager().getCurrentPage() * getPager().getPageRecord(), getPager()
				.getPageRecord(), cri, new String[] { sortBy + " " + sortByType, "oid desc" });
		logger.debug("pager:{}", getPager());
		return "list";
	}

	public String cancel() {
		if (StringUtils.isNotBlank(batchEventId)) {
			BatchEvent tmp = batchEventService.get(batchEventId);
			if (tmp != null) {
				if (tmp.isCancelable()) {
					tmp.setStatus(Status.CANCEL);
					batchEventService.update(tmp);
				} else {
					addActionError("與排程時間鄰近2分鐘, 無法取消");
					return list();
				}
			} else {
				addActionError(batchEventId + " 排程不存在");
			}
		}
		return list();
	}

	public String view() {
		if (StringUtils.isNotBlank(batchEventId)) {
			event = batchEventService.get(batchEventId);
			if (event != null) {
				List<BatchDetail> details = batchEventService.getDetails(batchEventId);
				getDetailMap().clear();
				for (BatchDetail d : details) {
					getDetailMap().put(d.getDataKey(), d);
				}
				return "view";
			}
		}
		addActionError("狀態錯誤");
		return list();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.common.web.AbstractAction#prepare()
	 */
	@Override
	public void prepare() throws Exception {
		super.prepare();
		getPager();
		// getPager().setPageRecord(50);

		actorId = AdminHelper.getUser().getUsername();

		if (menuTime == null) {
			menuTime = new ArrayList();
			for (int i = 0; i < 24; i++) {
				menuTime.add((i < 10 ? "0" + i : i) + ":00");
			}
		}

		if (menuPM == null) {
			menuPM = userService.getByRole(Role.Default.PM.name(), Role.Type.OTHER);
		}

		if (memuType == null) {
			memuType = menuService.get("BatchEvent.name");
		}

		if (menuStatus == null) {
			menuStatus = Arrays.asList(BatchEvent.Status.FINISH, BatchEvent.Status.QUEUE, BatchEvent.Status.END,
					BatchEvent.Status.RUNING, BatchEvent.Status.CANCEL, BatchEvent.Status.FAIL);
		}
	}

	/**
	 * @return the menuService
	 */
	public MenuService getMenuService() {
		return menuService;
	}

	/**
	 * @return the paramEventId
	 */
	public String getParamEventId() {
		return paramEventId;
	}

	/**
	 * @param paramEventId the paramEventId to set
	 */
	public void setParamEventId(String paramEventId) {
		this.paramEventId = paramEventId;
	}

	/**
	 * @return the paramStatus
	 */
	public BatchEvent.Status getParamStatus() {
		return paramStatus;
	}

	/**
	 * @param paramStatus the paramStatus to set
	 */
	public void setParamStatus(BatchEvent.Status paramStatus) {
		this.paramStatus = paramStatus;
	}

	/**
	 * @return the paramBatchName
	 */
	public String getParamBatchName() {
		return paramBatchName;
	}

	/**
	 * @param paramBatchName the paramBatchName to set
	 */
	public void setParamBatchName(String paramBatchName) {
		this.paramBatchName = paramBatchName;
	}

	/**
	 * @return the sortBy
	 */
	public String getSortBy() {
		return sortBy;
	}

	/**
	 * @param sortBy the sortBy to set
	 */
	public void setSortBy(String sortBy) {
		this.sortBy = sortBy;
	}

	/**
	 * @return the sortByType
	 */
	public String getSortByType() {
		return sortByType;
	}

	/**
	 * @param sortByType the sortByType to set
	 */
	public void setSortByType(String sortByType) {
		this.sortByType = sortByType;
	}

	/**
	 * @return the paramCreateDTStart
	 */
	public Date getParamCreateDTStart() {
		return paramCreateDTStart;
	}

	/**
	 * @param paramCreateDTStart the paramCreateDTStart to set
	 */
	public void setParamCreateDTStart(Date paramCreateDTStart) {
		this.paramCreateDTStart = paramCreateDTStart;
	}

	public String getParamCreateDTStartD() {
		if (paramCreateDTStart != null) {
			return new SimpleDateFormat("yyyy-MM-dd").format(paramCreateDTStart);
		}
		return "";
	}

	public String getParamCreateDTStartT() {
		if (paramCreateDTStart != null) {
			return new SimpleDateFormat("HH:mm").format(paramCreateDTStart);
		}
		return "";
	}

	public void setParamCreateDTStartD(String date) throws ParseException {
		if (StringUtils.isNotBlank(date)) {
			if (paramCreateDTStart == null) {
				paramCreateDTStart = new SimpleDateFormat("yyyy-MM-dd").parse(date);
			} else {
				Calendar input = Calendar.getInstance();
				Calendar start = Calendar.getInstance();
				input.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(date));
				start.setTime(paramCreateDTStart);
				start.set(input.get(Calendar.YEAR), input.get(Calendar.MONTH), input.get(Calendar.DATE));
				paramCreateDTStart = start.getTime();
			}
		} else {
			paramCreateDTStart = null;
		}
	}

	public void setParamCreateDTStartT(String date) throws ParseException {
		if (StringUtils.isNotBlank(date)) {
			if (paramCreateDTStart == null) {
				paramCreateDTStart = new SimpleDateFormat("HH:mm").parse(date);
			} else {
				Calendar input = Calendar.getInstance();
				Calendar start = Calendar.getInstance();
				input.setTime(new SimpleDateFormat("HH:mm").parse(date));
				start.setTime(paramCreateDTStart);
				start.set(Calendar.HOUR_OF_DAY, input.get(Calendar.HOUR_OF_DAY));
				start.set(Calendar.MINUTE, input.get(Calendar.MINUTE));
				paramCreateDTStart = start.getTime();
			}
		}
	}

	/**
	 * @return the paramCreateDTEnd
	 */
	public Date getParamCreateDTEnd() {
		return paramCreateDTEnd;
	}

	/**
	 * @param paramCreateDTEnd the paramCreateDTEnd to set
	 */
	public void setParamCreateDTEnd(Date paramCreateDTEnd) {
		this.paramCreateDTEnd = paramCreateDTEnd;
	}

	public String getParamCreateDTEndD() {
		if (paramCreateDTStart != null) {
			return new SimpleDateFormat("yyyy-MM-dd").format(paramCreateDTEnd);
		}
		return "";
	}

	public String getParamCreateDTEndT() {
		if (paramCreateDTStart != null) {
			return new SimpleDateFormat("HH:mm").format(paramCreateDTEnd);
		}
		return "";
	}

	public void setParamCreateDTEndD(String date) throws ParseException {
		if (StringUtils.isNotBlank(date)) {
			if (paramCreateDTEnd == null) {
				paramCreateDTEnd = new SimpleDateFormat("yyyy-MM-dd").parse(date);
			} else {
				Calendar input = Calendar.getInstance();
				Calendar start = Calendar.getInstance();
				input.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(date));
				start.setTime(paramCreateDTEnd);
				start.set(input.get(Calendar.YEAR), input.get(Calendar.MONTH), input.get(Calendar.DATE));
				paramCreateDTEnd = start.getTime();
			}
		} else {
			paramCreateDTEnd = null;
		}
	}

	public void setParamCreateDTEndT(String date) throws ParseException {
		if (StringUtils.isNotBlank(date)) {
			if (paramCreateDTEnd == null) {
				paramCreateDTEnd = new SimpleDateFormat("HH:mm").parse(date);
			} else {
				Calendar input = Calendar.getInstance();
				Calendar start = Calendar.getInstance();
				input.setTime(new SimpleDateFormat("HH:mm").parse(date));
				start.setTime(paramCreateDTEnd);
				start.set(Calendar.HOUR_OF_DAY, input.get(Calendar.HOUR_OF_DAY));
				start.set(Calendar.MINUTE, input.get(Calendar.MINUTE));
				paramCreateDTEnd = start.getTime();
			}
		}
	}

	/**
	 * @return the paramCreateUser
	 */
	public String getParamCreateUser() {
		return paramCreateUser;
	}

	/**
	 * @param paramCreateUser the paramCreateUser to set
	 */
	public void setParamCreateUser(String paramCreateUser) {
		this.paramCreateUser = paramCreateUser;
	}

	/**
	 * @return the memuType
	 */
	public Menu getMemuType() {
		return memuType;
	}

	/**
	 * @param memuType the memuType to set
	 */
	public void setMemuType(Menu memuType) {
		this.memuType = memuType;
	}

	/**
	 * @return the menuStatus
	 */
	public List<BatchEvent.Status> getMenuStatus() {
		return menuStatus;
	}

	/**
	 * @return the batchEvents
	 */
	public List<BatchEvent> getBatchEvents() {
		return batchEvents;
	}

	/**
	 * @param batchEvents the batchEvents to set
	 */
	public void setBatchEvents(List<BatchEvent> batchEvents) {
		this.batchEvents = batchEvents;
	}

	/**
	 * @return the event
	 */
	public BatchEvent getEvent() {
		return event;
	}

	/**
	 * @param event the event to set
	 */
	public void setEvent(BatchEvent event) {
		this.event = event;
	}

	/**
	 * @return the detailMap
	 */
	public Map<String, BatchDetail> getDetailMap() {
		if (detailMap == null) {
			detailMap = new HashMap();
		}
		return detailMap;
	}

	/**
	 * @param detailMap the detailMap to set
	 */
	public void setDetailMap(Map<String, BatchDetail> detailMap) {
		this.detailMap = detailMap;
	}

	/**
	 * @return the batchEventId
	 */
	public String getBatchEventId() {
		return batchEventId;
	}

	/**
	 * @param batchEventId the batchEventId to set
	 */
	public void setBatchEventId(String batchEventId) {
		this.batchEventId = batchEventId;
	}

	/**
	 * @return the menuTime
	 */
	public List<String> getMenuTime() {
		return menuTime;
	}

	/**
	 * @return the menuPM
	 */
	public List<User> getMenuPM() {
		return menuPM;
	}

	/**
	 * @return the actorId
	 */
	public String getActorId() {
		return actorId;
	}

	/**
	 * @param actorId the actorId to set
	 */
	public void setActorId(String actorId) {
		this.actorId = actorId;
	}

}
